<template>
  <div class="regulatory-page">
    <div class="return" @click="goBack">
      <Icon size="25" color="#000000" name="arrow-left" class="back" />
      <div class="title">
        <p class="text">全部评论</p>
      </div>
    </div>
    <div class="review" v-for="review in reviews" :key="review.reviewId">
      <div class="review-header">
        <VanImage
            round
            width="3rem"
            height="3rem"
            :src="require('@/assets/backgrounds/user.jpg')"
        />
        <div class="info">
          <p class="review-name">{{ review.username }}</p>
          <p class="review-date">{{ review.createTime }}</p>
        </div>
        <div class="rating">
          <Rate v-model="review.rating" readonly size="13" />
        </div>
      </div>
      <div class="review-text">{{ review.comment }}</div>
    </div>
  </div>
</template>

<script>
import {Icon, Image as VanImage, Rate} from 'vant';
import http from "@/plugins/http";

export default {
  name: "AllReviews",
  components: {Icon, VanImage, Rate },
  data() {
    return {
      reviews: []
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    fetchReviews(productId) {
      http.get(`/review/product/${productId}`)
          .then(response => {
            this.reviews = response.data || [];
          })
          .catch(error => {
            console.error("获取评价信息出错:", error);
          });
    }
  },
  activated() {
    const productId = this.$route.params.id;
    console.log("接收到的 productId:", productId); // 添加日志
    if (productId) {
      this.fetchReviews(productId);
    } else {
      console.error("未找到 productId");
    }
  }
};
</script>

<style scoped>
.regulatory-page {
  background-color: #F6F7F9;
  height: 100vh;
}

.return {
  background-color: #FAEAEA;
  padding: 15px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  margin-bottom: 10px;
}

.back {
  position: absolute;
  left: 10px;
  text-decoration: none;
}

.title {
  display: flex;
  align-items: center;
}

.text {
  margin: 0;
  color: #000000;
  font-size: 16px;
}
.review {
  padding: 10px;
  border-radius: 20px;
  background-color: white;
  margin-bottom: 10px;
  margin-left: 10px;
  margin-right: 10px;
}
.review-header {
  display: flex;
  align-items: flex-end;
  margin-bottom: 10px;
}
.info {
  margin-left: 10px;
  display: flex;
  flex-direction: column;
}
.review-name {
  font-weight: bold;
  margin: 0;
  font-size: 18px;
}
.review-date {
  color: #999;
  margin: 0;
}
.review-text {
  margin-top: 10px;
}
.rating {
  padding-left: 20px;
  padding-bottom: 23px;
}
</style>
